package com.common;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by 10742 on 2017/7/9.
 */
public class SampleInfoTool extends JdbcDaoSupport {

    public int insert(final String sequence, final String gene, final int moleculeID){
        final String sql = "insert into Sample_Info(Sequence, Gene, Sample_MeasureNO) values(?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, sequence);
                ps.setString(2, gene);
                ps.setInt(3, moleculeID);
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(int sampleID, String sequence, String gene){
        String sql = "update Sample_Info set Sequence=?,Gene=? where ID=?";
        return getJdbcTemplate().update(sql, sequence, gene, sampleID);
    }

}
